package com.zmn.oms.business.impl.order;

import com.alibaba.fastjson.JSON;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.consts.GlobalConsts;
import com.zmn.gms.common.dio.grid.online.engineer.EngineerBindGridDIO;
import com.zmn.gms.dubbo.interfaces.grid.online.engineer.GridEngineerListRemoteService;
import com.zmn.oms.business.interfaces.order.OrderServiceReasonBService;
import com.zmn.oms.common.dto.QueueOrderNodeNotifyDTO;
import com.zmn.oms.model.entity.detail.OrderDetail;
import com.zmn.oms.model.entity.order.OrderServiceReason;
import com.zmn.oms.services.interfaces.detail.OrderDetailService;
import com.zmn.oms.services.interfaces.order.OrderServiceReasonService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 类描述：
 *
 * @author wangxiaokun
 * @since 2022/03/29 11:35
 */
@Slf4j
@Service
public class OrderServiceReasonBServiceImpl implements OrderServiceReasonBService {

    @Autowired
    private OrderServiceReasonService orderServiceReasonService;
    @Autowired
    private OrderDetailService orderDetailService;

    @DubboReference(version = com.zmn.gms.dubbo.interfaces.utils.DubboConsts.INTERFACE_VERSION, check = false)
    private GridEngineerListRemoteService gridEngineerListRemoteService;

    @Override
    public void isSatisfyGrid(Long orderId, Long workId) {
        OrderDetail orderDetail = orderDetailService.findOrderDetailByKey(orderId);
        if (Objects.isNull(orderDetail) || NumberUtil.isNullOrZero(orderDetail.getGridId())) {
            return;
        }
        EngineerBindGridDIO dio = new EngineerBindGridDIO();
        dio.setGridId(orderDetail.getGridId());
        dio.setTypeId(String.valueOf(orderDetail.getServCategId()));
        ResponseDTO<Boolean> booleanResponseDTO = gridEngineerListRemoteService.gridIsBindEngineerByQuery(dio);
        log.info("[{}]查询网格是否绑定工程师入参：【{}】，出参：【{}】", orderDetail.getDetailId(), JSON.toJSONString(dio), JSON.toJSONString(booleanResponseDTO));
        if (booleanResponseDTO.isSuccess() && booleanResponseDTO.getData()) {
            return;
        }
        OrderServiceReason dbReason = orderServiceReasonService.findByKey(workId);
        if (Objects.isNull(dbReason)) {
            OrderServiceReason reason = new OrderServiceReason();
            reason.setWorkId(workId);
            reason.setOrderId(orderId);
            reason.setCreater("系统");
            reason.setUpdater("系统");
            reason.setSatisfyGrid(GlobalConsts.NO);
            orderServiceReasonService.insert(reason);
            return;
        }
        OrderServiceReason reason = new OrderServiceReason();
        reason.setWorkId(workId);
        reason.setSatisfyGrid(GlobalConsts.NO);
        orderServiceReasonService.updateByKey(reason);
    }
}
